#include "CPauseState.h"
#include "CMainMenuState.h"
#include "CCyberSpaceState.h"
#include "COptionsState.h"
#include "CSaveLoadState.h"
#include "Wrappers/CSGD_TextureManager.h"
#include "CPrintBitmapToScreen.h"
#include "CShopState.h"

CPauseState::CPauseState(void)
{
	m_nCurrentSelection = 0;
	m_nCursorID = -1;
	m_nBackgroundImageID = -1;
}

CPauseState::~CPauseState(void)
{

}

CPauseState* CPauseState::GetInstance()
{
	static CPauseState pauseState;
	return &pauseState;
}

void CPauseState::Enter(void)
{

	// Load images and sounds
	m_nCursorID = CSGD_TextureManager::GetInstance()->LoadTexture("resource/Graphics/SGP_Cursor.png");
}

bool CPauseState::Input(void)
{
	if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_UP)|| CSGD_DirectInput::GetInstance()->JoystickGetLStickDirPressed(DIR_UP))
	{
		if(m_nCurrentSelection > 0)
			--m_nCurrentSelection;
		else
			m_nCurrentSelection = 4;
	}

	if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_DOWN)|| CSGD_DirectInput::GetInstance()->JoystickGetLStickDirPressed(DIR_DOWN))
	{
		if(m_nCurrentSelection < 4)
			++m_nCurrentSelection;
		else
			m_nCurrentSelection = 0;
	}

	if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_RETURN)|| CSGD_DirectInput::GetInstance()->JoystickButtonPressed(0))
	{
		if(m_nCurrentSelection == 0)
		{
			CGame::GetInstance()->PopState();
		}
		else if(m_nCurrentSelection == 1)
		{
			CGame::GetInstance()->ChangeState(CMainMenuState::GetInstance());
		}
		else if(m_nCurrentSelection == 2)
		{
			CGame::GetInstance()->PushState(COptionsState::GetInstance());
		}
		
		else if (m_nCurrentSelection == 3)
		{
			CGame::GetInstance()->PushState(CShopState::GetInstance());
		}
		else if(m_nCurrentSelection == 4)
		{
			return false;
		}
	}

	// Exit the pause menu and return to previous screen
	if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_ESCAPE) || CSGD_DirectInput::GetInstance()->JoystickButtonPressed(6))
	{
		CGame::GetInstance()->PopState();
	}

	return true;
}

void CPauseState::Update(float fElapsedTime)
{

}

void CPauseState::Render(void)
{
	CPrintBitmapFontToScreen::GetInstance()->Print("RETURN TO GAME", 170, 240, 1.0f, D3DCOLOR_XRGB(255,0,0));
	CPrintBitmapFontToScreen::GetInstance()->Print("RETURN TO MENU", 170, 270, 1.0f, D3DCOLOR_XRGB(255,0,0));
	CPrintBitmapFontToScreen::GetInstance()->Print("OPTIONS", 170, 300, 1.0f, D3DCOLOR_XRGB(255,0,0));
	CPrintBitmapFontToScreen::GetInstance()->Print("SHOP", 170, 330, 1.0f, D3DCOLOR_XRGB(255,0,0));
	CPrintBitmapFontToScreen::GetInstance()->Print("EXIT GAME", 170, 360, 1.0f, D3DCOLOR_XRGB(255,0,0));

	// Temporary functionality display
	CSGD_Direct3D::GetInstance()->DrawTextA("The game is paused.", 200, 50, 255, 0, 0);

	// Draw cursor
	CSGD_TextureManager::GetInstance()->Draw(m_nCursorID, 130, 250 + m_nCurrentSelection*30);
}

void CPauseState::Exit(void)
{
	CSGD_TextureManager::GetInstance()->UnloadTexture(m_nCursorID);
}